<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
      class="search-btn"
      slot="title"
      round
      type="info"
      size="small"
      icon="search"
      to="/search"
      >搜索</van-button>
    </van-nav-bar>
  <van-tabs
    class="tabs"
    v-model="active"
    animated
    swipeable
    swipe-threshold="3"
  >
    <van-tab
    class="tab-item"
    :title="item.name"
    v-for="item in channelList"
    :key="item.id"
    >
      <ArticleList :channel="item"/>
    </van-tab>
    <van-icon
      class="more-btn"
      slot="nav-right"
      name="more-o"
      @click="showPopup"
    ></van-icon>
  </van-tabs>
  <!-- 频道编辑弹出层 -->
  <van-popup
    class="pop-toast"
    v-model="isChannelEditShow"
    position="bottom"
    round
    overlay
    closeable
    close-icon-position="top-left"
  >
    <ChannelEdit
      :myChannels="channelList"
      :active="active"
      @updateActive="handleUpdateActive"
    />
  </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '../../api/user'
import ArticleList from '@/views/home/components/article-list'
import ChannelEdit from '@/views/home/components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: { ArticleList, ChannelEdit },
  created () {
    this.loadChannels()
  },
  data () {
    return {
      active: 0, // 用于控制点哪个tab就展示相对应的tab内容
      channelList: [], // 存放我的频道列表
      isChannelEditShow: false // 展示弹出层
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async loadChannels () {
      if (this.user) { // 已登录状态,从后端获取用户频道
        try {
          const { data } = await getUserChannels()
          this.channelList = data.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      } else { // 未登录，从本地获取
        const data = getItem('UNLOGIN_CHANNELS')
        if (data) { // 本地中有数据
          this.channelList = JSON.parse(data)
        } else { // 本地中没有数据，则请求默认频道
          const { data } = await getUserChannels()
          this.channelList = data.data.channels
        }
      }
    },
    showPopup () {
      this.isChannelEditShow = true
    },
    handleUpdateActive (index, isShowEdit) {
      this.active = index
      this.isChannelEditShow = isShowEdit
    }
  }
}
</script>

<style scoped lang="less">
  .home-container {
    padding-top: 175px;
    .page-nav-bar {
      .van-nav-bar__title {
        max-width: unset !important;
        margin: 0 !important;
      }
      .search-btn {
        padding: 0 170px 0 170px;
        font-size: 28px;
        background-color: #5babfb;
        .van-icon {
          font-size: 32px;
          color: #fff;
        }
      }
    }
    /deep/ .tabs {
      height: 80px;
      .van-tabs__wrap {
        position: fixed;
        top: 92px;
        left: 0;
        right: 40px;
        height: 82px;
        z-index: 1;
        border-bottom: 1px solid #edeff3;
      }
      .van-tab {
        position: relative;
        min-width: 200px;
        font-size: 30px;
        color: #777777;
        border-right: 1px solid #edeff3;
      }
      .van-tab--active {
        color: #333;
      }
      .van-tabs__line {
        width: 35px;
        height: 6px;
        background-color: #3296fa;
      }
      .more-btn {
        position: fixed;
        right: 0;
        width: 60px;
        text-align: center;
        line-height: 80px;
        font-size: 40px;
        color: #333333;
        background-color: #fff;
        opacity: 0.902;
        border-left: 1px solid #c8c8c8;
      }
    }
    .pop-toast {
      height: 100%;
      .van-popup__close-icon {
        color: #222;
      }
    }
  }
</style>
